package com.wangshuqi.jdbc.pool;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

public class C3p0DataSource {

    private static DataSource cpds = new ComboPooledDataSource("helloc3p0");

    public static Connection getConnection2() throws SQLException {
        Connection connection = cpds.getConnection();
        return connection;
    }

    // 不推荐硬编码方式
    public static Connection getConnection() throws PropertyVetoException, SQLException {
        ComboPooledDataSource cpds = new ComboPooledDataSource();
        cpds.setDriverClass("com.mysql.jdbc.Driver");
        cpds.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        cpds.setUser("root");
        cpds.setPassword("123456");
        Connection connection = cpds.getConnection();
        return connection;
    }

    @Test
    public void test() {
        try {
//            Connection connection = getConnection();
//            System.out.println(connection);
            Connection connection2 = getConnection2();
            System.out.println(connection2);
        }catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
